using System;
using UnityEngine;
using UnityEngine.UI;

namespace Lean.Localization
{
	[ExecuteInEditMode]
	[DisallowMultipleComponent]
	[RequireComponent(typeof(Text))]
	public class LeanLocalizedTextArgs : LeanLocalizedBehaviour
	{
		[Tooltip("If PhraseName couldn't be found, this text will be used")]
		public string FallbackText;

		[NonSerialized]
		public object[] Args;

		public void SetArg(object arg, int index)
		{
			if (index >= 0)
			{
				bool flag = false;
				if (Args == null)
				{
					Args = new object[index + 1];
					flag = true;
				}
				else if (index >= Args.Length)
				{
					Array.Resize(ref Args, index + 1);
					flag = true;
				}
				if (Args[index] != arg)
				{
					Args[index] = arg;
					flag = true;
				}
				if (flag)
				{
					UpdateLocalization();
				}
			}
		}

		public override void UpdateTranslation(LeanTranslation translation)
		{
			Text component = GetComponent<Text>();
			if (translation != null)
			{
				if (Args != null)
				{
					component.text = string.Format(translation.Text, Args);
				}
				else
				{
					component.text = translation.Text;
				}
			}
			else if (Args != null)
			{
				component.text = string.Format(FallbackText, Args);
			}
			else
			{
				component.text = FallbackText;
			}
		}

		protected virtual void Awake()
		{
			if (string.IsNullOrEmpty(FallbackText))
			{
				Text component = GetComponent<Text>();
				FallbackText = component.text;
			}
		}
	}
}
